import requests
import json as json_obj
from core.tools.encryption import get_token as md5
from urllib import parse
from finance_c2c.settings import WmAppKey, WmAppSecret, url


class BaseWoMai(object):
    method = None
    param = None
    appkey = WmAppKey
    appsecret = WmAppSecret
    url = url

    def _get_method(self):
        assert self.method is not None, (
            "'%s' should either include a `method` attribute. "
            % self.__class__.__name__
        )
        return self.method

    def _get_param(self):
        if self.param:
            if isinstance(self.param, dict):
                return str(self.param)
            return self.param
        else:
            return ""

    @property
    def sign(self):
        method = self._get_method()
        param = self._get_param()
        return md5(method + self.appkey + self.appsecret + param)

    def my_requests_post(self, data=None, json=None, **kwargs):
        method = self._get_method()
        base_data = {
            "appkey": self.appkey,
            "method": method,
        }
        if json:
            base_data["param"] = parse.quote(json)
            base_data["sign"] = self.sign
            response = requests.post(self.url, verify=False, json=base_data, **kwargs)
        else:
            self.param = data
            data = self._get_param()
            base_data["sign"] = self.sign
            if data:
                base_data["param"] = parse.quote(data)
            response = requests.post(self.url, verify=False, data=base_data, **kwargs)
        response = response.content
        context = json_obj.loads(response)
        return context


class WoMaiApi(BaseWoMai):
    def get_goods_pool(self):
        # 3.1   商品页码API
        self.method = "womai.itempagenum.get"
        content = self.my_requests_post()
        return content

    def get_goods_num(self, pagenum):
        """
        :param pagenum:
        :return:
        """
        # 3.2.1    获取商品编号信息
        self.method = "womai.itemlist.get"
        param = {"pagenum": pagenum}
        context = self.my_requests_post(data=param)
        return context

    def get_goods_detail(self, skuid):
        # 3.3   商品详情API
        self.method = "womai.itemdetail.get"
        param = {
            "skuid": skuid
        }
        context = self.my_requests_post(data=param)
        return context

    def get_goods_image(self, skuid):
        # 3.5   商品图片API
        self.method = "womai.itemimage.get"
        param = {
            "skuid": skuid
        }
        context = self.my_requests_post(data=param)
        return context

    def get_goods_valuate(self):
        # 3.6  评论api
        self.method = "womai.itemevaluate.get"
        param = {
            "skuid": "958691"
        }
        context = self.my_requests_post(data=param)
        return context

    def get_goods_price(self):
        self.method = "womai.price.get"
        param = {
            "skuids": "958691"
        }
        context = self.my_requests_post(data=param)
        return context

    # 4.1.1  添加订单
    def goods_add(self, data):
        self.method = "womai.trade.add"
        context = self.my_requests_post(data=data)
        return context

    # 确认预订单
    def goods_trade_confirm(self, tid):
        """
        确认预订单
        :param tid: 主订单id
        :return:
        """
        self.method = "womai.trade.confirm"
        param = {
            "supplierorderid": tid
        }
        context = self.my_requests_post(data=param)
        return context

    # 取消订单
    def goods_trade_cancel(self, tid):
        """
        取消订单
        :param tid: 主订单id
        :return:
        """
        self.method = "womai.trade.cancel"
        param = {
            "supplierorderid": tid
        }
        context = self.my_requests_post(data=param)
        return context

    def after_sale(self, tid):
        """
        4.5   售后单申请API
        :param tid:
        :return:
        """

    # 订单查询API
    def get_trade(self, tid):
        """
        4.4   订单查询API
        :param tid:
        :return:
        """
        param = {
            "supplierorderid": tid
        }
        self.method = "womai.trade.get"
        context = self.my_requests_post(data=param)
        return context

    # 获取物流信息
    def get_logistics(self, tid):
        """
        :param tid:
        :return:
        """
        self.method = "womai.logistics.get"
        param = {
            "tid": tid
        }
        context = self.my_requests_post(data=param)
        return context

    # 获取库存(根据省份)
    def get_inventory(self, skuids_str, area):
        """
        获取库存(根据省份)
        :return:
        """
        self.method = "womai.inventory.get"
        param = {
            "warehouseid": "",
            "skuids": skuids_str,
            "area": area
            # "skuids": "958866",
            # "area": "3"
        }
        context = self.my_requests_post(data=param)
        return context

    def get_area(self, area_type):
        """
        5.1.1  获取地区信息
        area_type: 地区信息
        :return:
        """
        self.method = "womai.area.get"
        param = {
            "area_type": area_type,
        }
        context = self.my_requests_post(data=param)
        return context

    def get_area_vaild(self, province, city, county, goodstype):
        """
        5.2  查询配送范围是否有效
        :return:
        """
        self.method = "womai.area.vaild.get"
        param = {
            "province": province,
            "city": city,
            "county": county,
            "goodstype": goodstype,
        }
        content = self.my_requests_post(data=param)
        return content

    def get_itemstatus(self, skuids):
        """
                5.2  查询配送范围是否有效
                :return:
                """
        self.method = "womai.itemstatus.get"
        param = {
            "skuid": skuids
        }
        content = self.my_requests_post(data=param)
        return content


if __name__ == '__main__':
    pass
    # content = WoMaiApi().get_goods_pool()
    content = WoMaiApi().get_logistics("19121016442326836688")
    # content = WoMaiApi().get_goods_num(1)
    print(content)
    # for i in ["54410", "661681"]:
    #     content = WoMaiApi().get_goods_detail(i)
    #     print(content)
    # 库存
    # for i in ["1", "2"]:
    #     content = WoMaiApi().get_inventory("584940", i)
    #     print(content)
    # 取消订单
    # content = WoMaiApi().goods_trade_cancel("201905081122006024563013")
    # print(content)
    # 获取详情
    # content = WoMaiApi().get_goods_detail("681296")
    # print(content)